// pages/control/control.js
Page({
  data: {
    luminance:0,
  },
  
  onLoad: function (options) {
    this.apoints()  //这个是我获取onenet数据的函数
    var that=this
    setInterval(function(){
      that.apoints();
    },3000   //这里我设置3秒刷新一次
    )

  },

  apoints:function(e) {             //查询设备详情的函数
    var that = this
    wx.request({
      url: 'http://api.heclouds.com/devices/959631127/datapoints?', //xxz这里填写你的设备id
      //api-key
      header:{
        "api-key":"fcKXUbPreyZme5luUoKBKDb=fNI="  //这里写你的api-key
      },
      data:{
        limit:1    //数据流最新的一个数据 如果是5，即五个数据
      },
      method :"GET",
       //获取成功
      success:function(res){
        console.log(res)
       that.setData({
        luminance:res.data.data.datastreams[0]
        
       })    
      }
    })
  },
  
  openled:function(e){
    wx.request({
      url: 'http://api.heclouds.com/cmds?device_id=959631127',//*号这里写你设备id
      //设备ID
      //api-key
      header:{
        'content-type':'application/json',
        "api-key":"fcKXUbPreyZme5luUoKBKDb=fNI="  //这里写你的api-key
      },

      method :"POST",
      data:1,//数据1是为灯开
      success(res){
        console.log("控制成功,已开灯")
        console.log(res)
        console.log(res.data);

      }

    })
  },

  closeled:function(e){
    wx.request({
      url: 'http://api.heclouds.com/cmds?device_id=959631127',//*号这里写你设备id
      //设备ID
      //api-key
      header:{
        'content-type':'application/json',
        "api-key":"fcKXUbPreyZme5luUoKBKDb=fNI="  //这里写你的api-key
      },

      method :"POST",
      data:0,//数据1是为灯开
      success(res){
        console.log("控制成功,已关灯")
        console.log(res)
        console.log(res.data);

      }

    })
  },

  uploaddd:function(e) {
    var that = this
    wx.request({
      url: 'http://api.heclouds.com/devices/959631127/datapoints', //xxz这里填写你的设备id
      //api-key
      body:{
        "api-key":"fcKXUbPreyZme5luUoKBKDb=fNI="  //这里写你的api-key
      },
      "datastreams": [{
        "id": "luminance",
        "datapoints": [{
            "at": "2022-06-17T00:35:43",
            "value": "556"
          },
          {
            "at": "2013-04-22T00:55:43",
            "value": 84
          }
        ]
      },
      {
        "id": "open",
        "datapoints": [{
            "at": "2013-04-22T00:35:43",
            "value": 1
          },
          {
            "at": "2013-04-22T00:35:43",
            "value": 23.001
          }
        ]
      }
    ],
        
    method :"POST",
    
      success:function(res){
        console.log(res)
       that.setData({
        
        
       })    
      }
    })
  },

})